<?php
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use QL\QueryList;
use think\Controller;
use think\db;


class collect extends Command
{
    protected function configure()
    {
        $this->setName('collect')
            ->setDescription('collect');
    }

    protected function execute(Input $input, Output $output)
    {

        $url = 'https://www.ifanr.com/';

        $ql = QueryList::get($url);

        $url = $ql->find('.js-title-transform')->eq(0)->attr('href');

        if($url){
            $ql = QueryList::get($url);
            $title = $ql->find('h1')->html();
            $content = $ql->find('article')->html();


            $db = [
                // 数据库类型
                'type'        => 'mysql',
                // 服务器地址
                'hostname'    => '127.0.0.1',
                // 数据库名
                'database'    => 'boke_go12_top',
                // 数据库用户名
                'username'    => 'boke_go12_top',
                // 数据库密码
                'password'    => 'EW42TR4DpA8pX2Pj',
                // 数据库编码默认采用utf8
                'charset'     => 'utf8',
            ];

            $row =  Db::connect($db)->table('wp_posts')->where('cj_url',$url)->count();

            if($row){
                $output->writeln("已采集");
                return;
            }

            $content = str_replace('爱范儿经授权发布，文章为作者观点，不代表爱范儿立场','',$content);

            $id =  Db::connect($db)->table('wp_posts')->insertGetId([
                'post_author'=>1,
                'post_date'=>date('Y-m-d H:i:s'),
                'post_date_gmt'=>date('Y-m-d H:i:s'),
                'post_content'=>$content,
                'post_title'=>$title,
                'post_status'=>'publish',
                'comment_status'=>'open',
                'ping_status'=>'open',
                'post_type'=>'post',
                'post_name'=>urlencode($title),
                'cj_url'=>$url
            ]);

            if($id){
                Db::connect($db)->table('wp_posts')->where('ID',$id)->update([
                    'guid' => 'http://www.ww01.net/?p='.$id
                ]);
                Db::connect($db)->table('wp_term_relationships')->insert([
                    'object_id'=>$id,
                    'term_taxonomy_id'=>'12',
                ]);
                $output->writeln("采集".$title);
            }
        }

    }

}